home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8558 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  84 lines

  1. Newsgroups: comp.lang.c++
  2. Path: lut.ac.uk!usenet
  3. From: COSJM1 <S.J.Morgan-92@student.lut.ac.uk>
  4. Subject: Problem with Templates
  5. Sender: usenet@lut.ac.uk (Usenet-News)
  6. Message-ID: <DMy82D.Irt@lut.ac.uk>
  7. Date: Sun, 18 Feb 1996 01:56:37 GMT
  8. X-Nntp-Posting-Host: co-pclab39.lut.ac.uk
  9. Content-Transfer-Encoding: 7bit
  10. Content-Type: text/plain
  11. Mime-Version: 1.0
  12. X-Mailer: Mozilla 1.2N (Windows; I; 16bit)
  13. Organization: Loughborough University
  14.  
  15. Hi,
  16.     I'm currently writing my final year project in C++. It's spread
  17. over 17 modules and when using templates for a linked list class, it
  18. compiles alright but won't link correctly. The code has to be portable
  19. so I've tryed CFront 3.0, Borland 3.1,4.x and GCC 2.7.0 on a Sun Sparc10:
  20. they all give the same sort of link error.
  21.  
  22. Below is a small test program which demonstrates the priniples:
  23.  
  24. IN FILE "TEMPLATE.H":
  25.  
  26. template<class T> class A
  27. {
  28. public:
  29.     A() {}
  30. private:
  31.  
  32. };
  33.  
  34. template<class T> class B
  35. {
  36. public:
  37.     B() {}
  38.     void test(void);
  39.  
  40. private:
  41.     A<T>                     *include_one;
  42. };
  43.  
  44. IN FILE "TEMPLATE2.CPP":
  45.  
  46. #include <iostream.h>
  47. #include <template.h>
  48.  
  49. template<class T> void B<T>::test(
  50.         void )
  51. {
  52.     cout << "hello";
  53. }
  54.  
  55. IN FILE "TEMPLATE.CPP":
  56.  
  57. #include <iostream.h>
  58.  
  59. #include "template.h"
  60.  
  61. main()
  62. {
  63. B<int*>
  64.     test;
  65.  
  66.     test.test();
  67. }
  68.  
  69. I get the following linking error:
  70.  
  71. (Error) Undefined symbol(s)
  72.     B<int*>::test(void), referred to from template.o
  73.  
  74. I think it's to do with the scope when it gets instantiated because when I
  75. include all the above in one file, it compiles and links correctly. Obviously
  76. with 17 modules I don't want to put over 5K lines of source into one file!
  77.  
  78. I would appreciate any advice on how best to proceed.
  79.  
  80. Thank you in advance for any help given.
  81.  
  82. Steve.
  83.  
  84.